package wangjg.coco.module;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaProject;

import wangjg.coco.PDUtil;
import wangjg.coco.PDUtil.FileClassLoader;
import wangjg.mda.context.ModelContext;
import wangjg.mda.model.obj.Model;

public class Source2SourceProcessor extends BasicProcessor {
	private List<IResource> resourceList;
	private boolean separate = false;
	
	public Source2SourceProcessor() {
	}

	public void setSeparate(boolean separate) {
		this.separate = separate;
	}

	public void process(){
			ModelContext context = this.getModelContext();
			
			List<Model> modelList = new ArrayList<Model>();
	
			List<IFile> list = this.getFileList();
			for(IFile iFile:list){
				try {
					File file = iFile.getLocation().toFile();
					String charset = iFile.getCharset();
					
					context.setCharset(charset);
					
					Model model = context.loadModel(file);
					modelList.add(model);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			this.pretreatment(modelList);
			
			this.setTool("CONTEXT", context);
			this.gen(modelList, separate);
			
			System.out.println("Over.");
	//		MessageDialog.openWarning(null, "ok", "完成!");
		}

	public void _test(){
		try {
			IJavaProject jp = PDUtil.getJavaProject(resourceList.get(0));
			FileClassLoader cl = PDUtil.getClassLoader(jp);
			Object o = cl.findClass("test.Hello").newInstance();
			System.out.println("newInstance="+o);
			Method method = o.getClass().getMethod("hello");
			method.invoke(o);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
